package com.dq.demos.web.controller;


import com.alibaba.fastjson.JSONObject;
import com.dq.demos.web.pojo.ThirdUserBo;
import com.dq.demos.web.service.IYFKService;
import com.dq.demos.web.utils.HttpClientUtil;
import com.dq.demos.web.utils.ReturnDataUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/yfkToBTService")
public class HandlerYFKController {

    private static Logger logger = LoggerFactory.getLogger(HandlerYFKController.class);
    @Autowired
    private IYFKService yfkService;
    @Autowired
    private ThirdUserBo thirdUserBo;
    /**
     *  跳板服务器请求友费控
     * @param param
     * @return
     * @throws Exception
     */
    @RequestMapping("/tbYFK")
    public String requestYFKService(@RequestBody String param) throws  Exception{
        String result = "";
        logger.info("#########--begin--开始调用跳转服务器转发友费控");
        try{
            JSONObject requestParam = JSONObject.parseObject(param);
            this.checkGetInterfaceParams(requestParam);
            String toKenURL = this.thirdUserBo.getBaseURL()+this.thirdUserBo.getSuffixTokenURL();
            String token = this.yfkService.getToken(toKenURL,
                    requestParam.getString("appKey"), requestParam.getString("appSecret"));
            HashMap<String, Object> acctokenmap = new HashMap<>();
            acctokenmap.put("access_token", token);
            Object bodyParam = requestParam.get("data");
            String bodyURL = requestParam.getString("ywURL");

            result = HttpClientUtil.doPost(bodyURL, null, acctokenmap, bodyParam);
            if (null == result || "".equals(result)) {
                throw new Exception("请求地址:["+bodyURL+"],query参数:["+acctokenmap.toString()+"],body参数:["+bodyParam.toString()+"],费控响应为空");
            }
        }catch (Exception e){
            logger.error("#########调用跳转服务器转发友费控异常异常信息:【"+e.getMessage()+"】");
            result = ReturnDataUtil.error(e.getMessage());
        }
        logger.info("#########--end--结束调用跳转服务器转发友费控");
        return result;
    }

    /**
     * 获取友费控token
     * @param param
     * @return
     * @throws Exception
     */
    @RequestMapping("/getYFKToken")
    public String getYFKControlAccessToken(@RequestBody String param) throws  Exception{
        String result = "";
        try{
            JSONObject requestParam = JSONObject.parseObject(param);
            this.checkGetToKenParams(requestParam);
            String toKenURL = thirdUserBo.getBaseURL()+thirdUserBo.getSuffixTokenURL();
            String token = this.yfkService.getToken(toKenURL,
                    requestParam.getString("appKey"), requestParam.getString("appSecret"));
            result = ReturnDataUtil.success(token,"操作成功");
        }catch(Exception e){
            result = ReturnDataUtil.error(e.getMessage());
        }
        return result;
    }

    /**
     * 检查获取友费控token参数
     * @param json
     * @throws Exception
     */
    private void checkGetToKenParams(JSONObject json) throws Exception{
        StringBuilder error = new StringBuilder();
        if(null == json.getString("appKey") || "".equals(json.getString("appKey"))){
            error.append("appKey不能为空 ");
        }
        if(null == json.getString("appSecret") || "".equals(json.getString("appSecret"))){
            error.append("appSecret不能为空 ");
        }
        if (error.toString().length() > 0) {
            throw new Exception(error.toString());
        }

    }

    /**
     * 检查获取友费控token参数
     * @param json
     * @throws Exception
     */
    private void checkGetInterfaceParams(JSONObject json) throws Exception{
        StringBuilder error = new StringBuilder();
        if(null == json.getString("appKey") || "".equals(json.getString("appKey"))){
            error.append("appKey不能为空 ");
        }
        if(null == json.getString("appSecret") || "".equals(json.getString("appSecret"))){
            error.append("appSecret不能为空 ");
        }
        if(null == json.getString("ywURL") || "".equals(json.getString("ywURL"))){
            error.append("业务接口地址不能为空,格式参照费控-请求地址");
        }
        if(null == json.getString("data") || "".equals(json.getString("data"))){
            error.append("业务接口请求数据不能为空,格式参照费控接口-body参数");
        }
        if (error.toString().length() > 0) {
            throw new Exception(error.toString());
        }

    }

}
